package org.mozhu.mboot.admin.maintain.notification.service;

import org.apache.commons.lang3.ArrayUtils;
import org.mozhu.mboot.admin.maintain.notification.entity.Notification;
import org.mozhu.mboot.admin.maintain.notification.repository.NotificationRepository;
import org.mozhu.mboot.admin.system.account.entity.User;
import org.mozhu.mboot.admin.system.account.service.UserService;
import org.mozhu.mboot.core.data.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 通知 - 业务层实现类
 *
 * @author ZhangPei
 */
@Transactional
@Service
public class NotificationServiceImpl extends BaseService<Notification, Long> implements NotificationService {

    @Autowired
    UserService userService;

    protected NotificationRepository getBaseRepository() {
        return (NotificationRepository) this.baseRepository;
    }

    @Override
    public long countUnread(Long currentUserId) {
        return this.getBaseRepository().countUnread(currentUserId);
    }

    @Override
    public void saveNotification(Long[] userIds, Notification m) {
        if (m == null) {
            return;
        }
        if (ArrayUtils.isNotEmpty(userIds)) {
            for (Long userId : userIds) {
                User user = userService.findById(userId);
                if (user == null) {
                    continue;
                }

                Notification clone = new Notification();
                clone.setTitle(m.getTitle());
                clone.setContent(m.getContent());
                clone.setDate(m.getDate());
                clone.setModule(m.getModule());
                clone.setRead(m.getRead());
                clone.setUserId(userId);

                save(clone);
            }
        }
    }

}
